﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QXBook.Model;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
using QXBook.Utils;

namespace QXBook.Service
{
    /// <summary>
    /// 收藏,不需要更新和获取某一个的方法
    /// </summary>
    public class FavoritesService : ServiceBase
    {
        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="AdminName"></param>
        /// <param name="article"></param>
        /// <returns></returns>
        public string AddFavorites( FavoritesInfo favor)
        {
          
            DbCommand cmd = new SqlCommand();
            cmd.CommandText = @"INSERT INTO Favorites(ProductID,UserID,AddDate) 
                                VALUES (@ProductID,@UserID,GETDATE());SELECT @@IDENTITY";//返回主键

            base.db.AddInParameter(cmd, "@ProductID", DbType.String, favor.ProductID);
            base.db.AddInParameter(cmd, "@UserID", DbType.String, favor.UserID);

            base.db.ExecuteNonQuery(cmd);
            return string.Empty;
        }

        public bool CheckFavorite(int productID, int userID)
        {
            DbCommand cmd = new SqlCommand();
            cmd.CommandText = @"SELECT COUNT(0) FROM Favorites WHERE ProductID=@ProductID AND UserID=@UserID";//返回主键

            base.db.AddInParameter(cmd, "@ProductID", DbType.String, productID);
            base.db.AddInParameter(cmd, "@UserID", DbType.String, userID);

            object result = base.db.ExecuteScalar(cmd);
            if (result != null)
            {
                return CommonUtils.ToInt(result) > 0;
            }
            return false;
        }

        /// <summary>
        /// 获取收藏列表
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<FavoritesInfo> GetModelList(ref PagerState ps, FavoritesCondition condition)
        {
            List<FavoritesInfo> list = new List<FavoritesInfo>();
            DbCommand cmd = new SqlCommand();

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("SELECT A.FavoritesID,A.ProductID,C.Title,B.Price,B.Sales,a.AddDate FROM Favorites A JOIN Product B ON A.ProductID=B.Product ");
            sbSql.Append(" JOIN CoreTemplate C ON B.CoreID=C.CoreID ");
            sbSql.Append(" WHERE 1=1");
            if (condition != null)
            {
                if (condition.ProductID > 0)
                {
                    sbSql.Append(" AND A.ProductID=" + condition.ProductID);
                }

                if (condition.UserID > 0)
                {
                    sbSql.Append(" AND A.UserID=" + condition.ProductID);
                }
            }

            DataTable dt = this.GetListPager(ref ps, sbSql.ToString(), "FavoritesID", cmd);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    FavoritesInfo art = new FavoritesInfo();
                    art.FavoritesID = CommonUtils.ToInt(dr["FavoritesID"]);
                    art.ProductID = CommonUtils.ToInt(dr["ProductID"]);

                    art.Product = new ProductInfo();
                    art.Product.ProductID = art.ProductID;
                    art.Product.Title = CommonUtils.ToString(dr["Title"]);
                    art.Product.Price = CommonUtils.ToDecimal(dr["Price"]);
                    art.Product.Sales = CommonUtils.ToInt(dr["Sales"]);
                    art.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                    list.Add(art);
                }
            }

            return list;
        }
    }
}
